﻿using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for WarrantyEditorView.xaml
    /// </summary>
    public partial class WarrantyEditorView
    {
        private readonly WarrantyController _warrantyControl;
        private readonly Warranty _warranty;

        public WarrantyEditorView()
        {
            InitializeComponent();

            _warrantyControl = new WarrantyController();
            Title = "Add Warranty";
        }

        public WarrantyEditorView(Warranty warranty)
        {
            InitializeComponent();
            _warrantyControl = new WarrantyController();
            Title = "Edit Warranty";

            _warranty = warranty;

            textBoxId.Text = warranty.Id.ToString();
            textBoxType.Text = warranty.Type;
        }

        private void ClearTextBox()
        {
            textBoxId.Text = "";
            textBoxType.Text = "";
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxType.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else
            {
                if (Title.Equals("Add Warranty"))
                {
                    var warranty = new Warranty
                                     {
                                         Type = textBoxType.Text
                                     };

                    var message = _warrantyControl.AddWarranty(warranty);

                    switch (message)
                    {
                        case 1:
                            MessageBox.Show("sukses menambah warranty");
                            ClearTextBox();
                            break;
                        default:
                            MessageBox.Show("type sudah ada");
                            break;
                    }
                }
                else
                {
                    var newWarranty = new Warranty
                                        {
                                            Id = _warranty.Id,
                                            Type = textBoxType.Text
                                        };

                    var message = _warrantyControl.EditWarranty(newWarranty, _warranty.Type);

                    switch (message)
                    {
                        case 1:
                            MessageBox.Show("sukses mengedit warranty");
                            break;
                        default:
                            MessageBox.Show("type sudah ada");
                            break;
                    }
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
